package controllers

import (
	"xxp_api/models"

	"github.com/astaxie/beego"
)

// 店铺管理接口
type ShopController struct {
	beego.Controller
}

// @Summary 商铺信息
// @Description 商铺信息
// @Param	uid 		    query 	int	true	 "商铺用户id"
// @Success 200 {object} models.Response
// @router /detail [post]
func (u *ShopController) Detail() {
	var ret models.Response

	uid, err := u.GetInt("uid")
	beego.Debug(u.Ctx.Request.Form)
	if err != nil || uid <= 0 {
		beego.Debug("参数错误uid:", uid)
		ret.Code = models.ERRCODE_PARAMS_ERROR
		ret.Msg = "参数错误"
	} else {
		ret = models.ShopDetail(uid)
	}

	u.Data["json"] = ret
	u.ServeJSON()
}

// @Summary 更新商铺信息，可单一更改
// @Description 商铺信息
// @Param	uid 		      query 	int	true	 "用户id"
// @Param	name 		query  string	true	 "商铺名称"
// @Param	description   string	true	 "商铺介绍"
// @Param	head_img 	  string	true	 "商铺头像"
// @Success 200 {object} models.Response
// @router /update [post]
func (u *ShopController) Update() {
	var ret models.Response
	uid, err := u.GetInt("uid")
	name := u.GetString("name")
	description := u.GetString("description")
	head_img := u.GetString("head_img")

	beego.Debug(u.Ctx.Request.Form)
	if (name == "" && description == "" && head_img == "") || uid <= 0 || err != nil {
		beego.Debug("参数错误name:", name, ",description:", description, ",head_img:", head_img)
		ret.Code = models.ERRCODE_PARAMS_ERROR
		ret.Msg = "参数错误"
	} else {
		ret = models.ShopUpdate(uid, name, description, head_img)
	}

	u.Data["json"] = ret
	u.ServeJSON()
}

// @Summary 店铺管理，商品列表
// @Description 店铺商品列表
// @Param	uid 	 query 	int	true	 "用户id"
// @Param	types    query  string	true	 "类型，1：兑换券，2线上商品，3线下课程"
// @Param	keystart 	    int	    true	 "分页参数"
// @Success 200 {object} models.Response
// @router /goods [post]
func (u *ShopController) Goods() {
	var ret models.Response
	uid, _ := u.GetInt("uid")
	types, _ := u.GetInt("types")
	keystart, _ := u.GetInt("keystart")
	beego.Debug(u.Ctx.Request.Form)
	if types <= 0 || uid <= 0 || keystart < 0 {
		ret.Code = models.ERRCODE_PARAMS_ERROR
		ret.Msg = "参数错误"
	} else {
		ret = models.ShopGoods(uid, types, keystart)
	}
	u.Data["json"] = ret
	u.ServeJSON()
}

// @Summary 添加商品置顶
// @Description
// @Param	uid 	 query 	int	true	 "用户id"
// @Param	gid    query  string	true	 "商品id"
// @Success 200 {object} models.Response
// @router /addtop [post]
func (u *ShopController) Addtop() {
	var ret models.Response
	uid, _ := u.GetInt("uid")
	gid, _ := u.GetInt("gid")
	beego.Debug(u.Ctx.Request.Form)
	if gid <= 0 || uid <= 0 {
		ret.Code = models.ERRCODE_PARAMS_ERROR
		ret.Msg = "参数错误"
	} else {
		ret = models.ShopGoodsAddTop(uid, gid)
	}
	u.Data["json"] = ret
	u.ServeJSON()
}

// @Summary 取消商品置顶
// @Description
// @Param	uid 	 query 	int	true	 "用户id"
// @Param	gid    query  string	true	 "商品id"
// @Success 200 {object} models.Response
// @router /deltop [post]
func (u *ShopController) Deltop() {
	var ret models.Response
	uid, _ := u.GetInt("uid")
	gid, _ := u.GetInt("gid")
	beego.Debug(u.Ctx.Request.Form)
	if gid <= 0 || uid <= 0 {
		ret.Code = models.ERRCODE_PARAMS_ERROR
		ret.Msg = "参数错误"
	} else {
		ret = models.ShopGoodsDelTop(uid, gid)
	}
	u.Data["json"] = ret
	u.ServeJSON()
}
